Voxels, meshes, streamlines, and connectomes¶
Display brain volumes with mesh overlays and connectivity data including adjustable edge and node scaling.
In [1]:
from pathlib import Path
from ipyniivue import download_dataset
BASE_API_URL = "https://niivue.com/demos/images/"
DATA_FOLDER = Path("images")
# Download data for example
download_dataset(
BASE_API_URL,
DATA_FOLDER,
files=[
"mni152.nii.gz",
"BrainMesh_ICBM152.lh.mz3",
"dpsv.trx",
"connectome.jcon",
],
)
mni152.nii.gz already exists. BrainMesh_ICBM152.lh.mz3 already exists. Downloading dpsv.trx...
Downloading connectome.jcon... Dataset downloaded successfully to images.
In [2]:
import ipywidgets as widgets
from IPython.display import display
from ipyniivue import NiiVue
## Initialize Viewer
nv = NiiVue()
nv.load_volumes([{"path": DATA_FOLDER / "mni152.nii.gz"}])
nv.load_meshes(
[
{
"path": DATA_FOLDER / "BrainMesh_ICBM152.lh.mz3",
"rgba255": [255, 64, 32, 255],
},
{"path": DATA_FOLDER / "dpsv.trx"},
{"path": DATA_FOLDER / "connectome.jcon"},
]
)
nv.set_mesh_shader(nv.meshes[0].id, "crosscut")
nv.set_clip_plane(-0.1, 270, 0)
nv.opts.mesh_xray = 0.02
## Create Interactive Controls
edge_slider = widgets.IntSlider(
value=10, min=5, max=30, description="Edge", readout=False
)
def on_edge_change(change):
"""Set edge diameter."""
nv.set_mesh_property(nv.meshes[2].id, "edge_scale", change["new"] * 0.1)
edge_slider.observe(on_edge_change, names="value")
node_slider = widgets.IntSlider(
value=10, min=5, max=30, description="Node", readout=False
)
def on_node_change(change):
"""Set node diameter."""
nv.set_mesh_property(nv.meshes[2].id, "node_scale", change["new"] * 0.1)
node_slider.observe(on_node_change, names="value")
## Display Viewer
controls = widgets.HBox([edge_slider, node_slider])
display(controls)
display(nv)